home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / delphi.swg / 0078_ENTER key instead of TAB.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-11-24  |  4.7 KB  |  168 lines

  1.  
  2. Here is something I picked up off Compuserve that should help.  I
  3. have a zip that contains examples of several other controls besides 
  4. the edit.  If anyone would like a copy of the zip, let me know and I 
  5. will send it to you.
  6.  
  7.  
  8. Using the <Enter> key like a <Tab> key with Delphi Controls
  9. ===========================================================
  10.  
  11. The example code supplied here demonstrates how to trap the 
  12. <Enter> key and the cursor keys to provide better data entry
  13. processing.
  14.  
  15. The trick is to overide the Keypress and KeyDown events so
  16. that they process the keys the way you want. In the examples
  17. supplied I have used the <Enter> key to move to the next 
  18. control (like the <Tab> key) and the cursor Up and Down keys
  19. to move to the previous and next controls respectively.
  20.  
  21. The Edit and EBEdit use the cursor keys as stated above, but
  22. the Combobox and the Listbox use Shift-Up and Shift-Down 
  23. instead so as not to interfere with existing functionality.
  24.  
  25. The Grid control uses the <Enter> key to move between fields,
  26. however it will not move from the last field of the last row.
  27. It is very easy to make it exit the grid at this point if you
  28. need to.
  29.  
  30. The method used to move to the next/previous control is the 
  31. Windows API call SendMessage which is used to dispatch a 
  32. WM_NEXTDLGCTL to the form the controls are children to. 
  33. Delphi provides a function called GetParentForm to get the 
  34. handle of the parent form of the control.
  35.  
  36. These simple extensions can be expanded to respond to almost
  37. any keyboard event, and I think using this method is less 
  38. trouble than trapping keys in the forms OnKey events (using
  39. keypreview:=true).
  40.  
  41. Feel free to use the code as you wish, but if you discover 
  42. something new please let me in on it!
  43.  
  44.  
  45. Simon Callcott
  46.  
  47. CIS: 100574,1034
  48.  
  49. {
  50.   Edit control that reponds as if the <Tab> key has been pressed when an
  51.   <Enter> key is pressed, moving to the next control.
  52.   Very simple extension to the KeyPress event, this technique should work
  53.   with TDBedit as well, Useful for data entry type apps.
  54.   Less trouble than using the Keypreview function of the form to do the same
  55.   thing.
  56.  
  57.   Please Use Freely.
  58.  
  59.   Simon Callcott  CIS: 100574, 1034
  60. }
  61.  
  62.  
  63. unit Entedit;
  64.  
  65. interface
  66.  
  67. uses
  68.   SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  69.   Forms, Dialogs, StdCtrls;
  70.  
  71. type
  72.   TEnterEdit = class(TEdit)
  73.   private
  74.  
  75.   protected
  76.  
  77.     procedure KeyPress(var Key: Char); override;
  78.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  79.  
  80.   public
  81.  
  82.   published
  83.  
  84.   end;
  85.  
  86. procedure Register;
  87.  
  88. implementation
  89.  
  90. procedure Register;
  91. begin
  92.   RegisterComponents('Samples', [TEnterEdit]);
  93. end;
  94.  
  95. procedure TEnterEdit.KeyPress(var Key: Char);
  96. var
  97.    MYForm: TForm;
  98. begin
  99.  
  100.    if Key = #13 then
  101.    begin
  102.        MYForm := GetParentForm( Self );
  103.        if not (MYForm = nil ) then
  104.            SendMessage(MYForm.Handle, WM_NEXTDLGCTL, 0, 0);
  105.        Key := #0;
  106.    end;
  107.  
  108.    if Key <> #0 then inherited KeyPress(Key);
  109.  
  110. end;
  111.  
  112. procedure TEnterEdit.KeyDown(var Key: Word; Shift: TShiftState);
  113. var
  114.    MYForm: TForm;
  115.    CtlDir: Word;
  116. begin
  117.  
  118.    if (Key = VK_UP) or (Key = VK_DOWN) then
  119.    begin
  120.        MYForm := GetParentForm( Self );
  121.        if Key = VK_UP then CtlDir := 1
  122.        else CtlDir :=0;
  123.        if not (MYForm = nil ) then
  124.            SendMessage(MYForm.Handle, WM_NEXTDLGCTL, CtlDir, 0);
  125.    end
  126.    else inherited KeyDown(Key, Shift);
  127.  
  128. end;
  129.  
  130. end.
  131.  
  132. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  133.  
  134. Q.  "Is there a way to use the return key for data entry, instead of tab or the
  135.     mouse?"
  136.  
  137. A.  Use this code for an Edit's OnKeyPress event.
  138.  
  139.     procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
  140.     begin
  141.       If Key = #13 Then
  142.       Begin
  143.         SelectNext(Sender as tWinControl, True, True );
  144.         Key := #0;
  145.       end;
  146.     end;
  147.  
  148.     This causes Enter to behave like tab.  Now, select all controls on the form
  149.     you'd like to exhibit this behavior (not Buttons) and go to the Object
  150.     Inspector and set their OnKeyPress handler to EditKeyPress.  Now, each
  151.     control you selected will process Enter as Tab.  If you'd like to handle
  152.     this at the form (as opposed to control) level, reset all the controls
  153.     OnKeyPress properties to blank, and set the _form_'s OnKeyPress property to
  154.     EditKeyPress.  Then, change Sender to ActiveControl and set the form's
  155.     KeyPreview property to true:
  156.  
  157.     procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
  158.     begin
  159.       If Key = #13 Then
  160.       begin
  161.         SelectNext(ActiveControl as tWinControl, True, True );
  162.         Key := #0;
  163.       end;
  164.     end;
  165.  
  166.     This will cause each control on the form (that can) to process Enter as Tab.
  167. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
  168.